/**
 * Created by Tofik Mamishov on 12.10.2014.
 */
var express = require('express');
var router = express.Router();
var Society = require('../libs/mongoose').Society;
var Account = require('../libs/mongoose').Account;
var SocietyAccount = require('../libs/mongoose').SocietyAccount;
var resHandler = require('../libs/resHandler').resHandler;
var accounts = {
    /**
     * path /accounts.get
     * params: profileId - id of profile in a society, society - id of society, name - a name of account (get
     * from a social network displayed name), role - role of account
     * @param req - request
     * @param res - response
     */
    get: function (req, res) {
        SocietyAccount.findOne({ profileId: req.body.profileId, _society: req.body.society })
            .populate('_account')
            .exec(function (err, socAcc) {
                if (!resHandler.isError(err, res)) {
                    if (socAcc != null) {
                        res.send({
                            socAcc: socAcc
                        });
                    } else {
                        var newAccount = new Account({
                            name: req.body.name,
                            role: req.body.role
                        });
                        newAccount.save(function (err) {
                            if (!resHandler.isError(err, res)) {
                                var newSocAcc = new SocietyAccount({
                                    _account: newAccount._id,
                                    _society: req.body.society,
                                    profileId: req.body.profileId
                                });
                                newSocAcc.save(function (err) {
                                    if (!resHandler.isError(err, res)) {
                                        SocietyAccount.findOne(newSocAcc)
                                            .populate('_account')
                                            .exec(function (err, socAcc) {
                                                if (!resHandler.isError(err, res)) {
                                                    res.send({
                                                        socAcc: socAcc
                                                    })
                                                }
                                            });
                                    }
                                });
                            }
                        });
                    }
                }
            });
    },
    /**
     * path /accounts.del
     * params: id - id of account
     * @param req - request
     * @param res - response
     */
    del: function (req, res) {
        Account.remove({ _id: req.body.id }, function (err) {
            if (!resHandler.isError(err, res)) {
                SocietyAccount.remove({ _account: req.body.id }, function (err) {
                    if (!resHandler.isError(err, res)) {
                        res.send({
                            deleted: req.body.id
                        });
                    }
                });
            }
        });
    },
    /**
     * path /accounts.edit
     * params: id - id of account, name - new name of account
     * @param req - request
     * @param res - response
     */
    edit: function (req, res) {
        Account.update({ _id: req.body.id }, { name: req.body.name }, {multi: true }, function (err, numAffected) {
            if (!resHandler.isError(err, res)) {
                res.send({
                    numAff: numAffected
                });
            }
        });
    }
};

module.exports.accounts = accounts;